<?php
/*
 * 给用户发消息提醒
 */
class Notice_Notification
{
    const _CONST_TB_DEFAULT_ = 'tg_users_message';
	private $_to, $_message;

	public function __construct ()
	{
	}

	public function to ($uid)
	{
		$this->_to = (int) $uid;
		return $this;
	}

	public function message ($message)
	{
		$this->_message = $message;
		return $this;
	}

	public function send ($message = NULL)
	{
		if (! empty($message))
			$this->_message = $message;

		if (empty($this->_to) || empty($this->_message))
			return FALSE;

        return $this->add(array(
            'uid'     => $this->_to,
            'message' => $this->_message,
            'status'  => Kohana::config('notice.notification.status.unread'),
            'ctime'   => DB::expr('unix_timestamp(current_timestamp())'),
        ));
	}

    /**
     * 用户当前有几条消息
     *
     * @param   int     $uid
     * @param   string  message status, unread on default
     * @return  int
     */
    public function count ($uid, $status = 'unread')
    {
        return DB::select(array('count("id")', 'count'))
            ->from(self::_CONST_TB_DEFAULT_)
            ->where('uid', '=', (int) $uid)
            ->where('status', 'in', $this->status_list($status))
            ->execute()
            ->get('count');
    }

    /**
     * 用户消息列表
     *
     * @param   int     $uid
     * @param   string  message status
     * @return  array
     */
    public function lists ($uid, $offset = 0, $limit = 10, $status = NULL)
    {
        $lists = DB::select()
            ->from(self::_CONST_TB_DEFAULT_)
            ->where('uid', '=', (int) $uid)
            ->where('status', 'in', $this->status_list($status))
            ->offset((int) $offset)
            ->limit ((int) $limit)
            ->order_by('ctime', 'DESC')
            ->execute()
            ->as_array();

        foreach ($lists as & $l)
            $l['message'] = utf8::substr($l['message'], 0, 28) . ' ...';
        return $this->build_status_name ($lists);
    }

    /**
     * @param   int     $uid
     * @param   int     message ID
     * @return  array
     */
    public function detail ($uid, $msgId)
    {
        $detail = DB::select()
            ->from(self::_CONST_TB_DEFAULT_)
            ->where('uid', '=', (int) $uid)
            ->where('id',  '=', (int) $msgId)
            ->execute()
            ->current();

        return $this->build_status_name ($detail);
    }

    /**
     * @param   int     $uid
     * @param   int     message ID
     * @return  array
     */
    public function set_read ($uid, $msgId)
    {
        return DB::update(self::_CONST_TB_DEFAULT_)
            ->set(array(
            'status' => Kohana::config('notice.notification.status.read')))
            ->where('uid', '=', (int) $uid)
            ->where('id',  '=', (int) $msgId)
            ->where('status', '=', Kohana::config('notice.notification.status.unread'))
            ->limit(1)
            ->execute();
    }

    protected function add ($record)
    {
        list($insert_id, $rows) = db::insert(self::_CONST_TB_DEFAULT_)
            ->columns (array_keys($record))
            ->values  (array_values ($record))
            ->execute();

        return $insert_id;
    }

    /**
     * @param   string  message status
     * @return  array
     */
    protected function status_list ($status)
    {
        if (empty($status))
            $arr_status = array(
                Kohana::config('notice.notification.status.unread'),
                Kohana::config('notice.notification.status.read'),
            );
        else
            $arr_status = array(
                Kohana::config("notice.notification.status.".$status),
            );

        return $arr_status;
    }

    private function build_status_name (& $record)
    {
        if (! isset($record['status']))
        {
            foreach ($record as & $rd)
                $this->build_status_name ($rd); 

            return $record;
        }
        else
        {
            $status_code = arr::get($record, 'status', 0);
            $name = Kohana::config('notice.notification.status_name.'.$status_code);
            $record['status_name'] = empty($name) ? '未知' : $name;
            return $record;
        }
    }
}
